﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu.tools
{
    public class SiteCollection
    {
        Hashtable hs_cname = new Hashtable();
        Hashtable hs_domain = new Hashtable();
        Hashtable hs_title = new Hashtable();
        List<string> list = new List<string>();
        SqlServerOperate sql = new SqlServerOperate();
        //构造函数
        public SiteCollection()
        {
            LoadSite();
        }
        //加载所有code
        private void LoadSite()
        {
            SqlDataReader re = sql.Query("select * from 全局设置.dbo.[信息平台设置] order by times desc,domain desc");
            Site site;
            while (re.Read())
            {
                site = new Site(re["wid"].ToString(), re["title"].ToString(), re["domain"].ToString(), re["typeid"].ToString(), Convert.ToInt32(re["btmaxlen"]), Convert.ToInt32(re["sleeptime"]), Convert.ToInt32(re["loginvcode"]), Convert.ToInt32(re["postvcode"]), re["class"].ToString());
                hs_cname.Add(site.ClassName, site);
                hs_domain.Add(site.Domain, site);
                hs_title.Add(site.Title, site);
                list.Add(site.Title + "(" + site.Domain + ")");
            }
            re.Close();
        }
        //获取一个网站信息
        public Site GetSiteByClassName(string cname)
        {
            if (hs_cname.Contains(cname))
            {
                return (Site)hs_cname[cname];
            }
            return null;
        }
        public Site GetSiteByDomain(string domain)
        {
            if (hs_domain.Contains(domain.ToString()))
            {
                return (Site)hs_domain[domain];
            }
            return null;
        }
        public Site GetSiteByName(string name)
        {
            if (hs_title.Contains(name.ToString()))
            {
                return (Site)hs_title[name];
            }
            return null;
        }
        //获取一个网站wid
        public string GetWidByName(string name)
        {
            if (hs_title.Contains(name))
            {
                Site site = (Site)hs_title[name];
                return site.Wid;
            }
            return null;
        }
        //获取所有网址
        public List<string> GetAllSite()
        {
            return list;
        }
        //加载所有网址
        public void LoadToComboBox(ComboBox cb)
        {
            cb.Items.Add("所有网址");
            foreach (string s in list)
            {
                cb.Items.Add(s);
            }
            cb.SelectedIndex = 0;
            //combobox绑定更改事件
            cb.SelectedIndexChanged += delegate
            {
                string domain = GetDomain(cb);
                if (domain != null)
                {
                    sql.Update("update 全局设置.dbo.[信息平台设置] set times=times+1 where domain='" + domain + "'");
                }
            };
        }
        //获取域名
        public string GetDomain(ComboBox cb)
        {
            if (cb.SelectedIndex > 0)
            {
                return Regex.Match(cb.SelectedItem.ToString(), "\\(([^\\)]+)\\)").Groups[1].Value;
            }
            return null;
        }
    }
}
